package com.example.oct.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.oct.mapper.QuestionnaireMapper;
import com.example.oct.pojo.Questionnaire;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class QuestionnaireService {

    // 以Map存储表名和保存content的队列
    private final Map<String, Queue<String>> addObj = new HashMap<>();

    @Resource
    private QuestionnaireMapper questionnaireMapper;

    public List<Questionnaire> queryQues(String sheetName) {
        return questionnaireMapper.queryQues(sheetName);
    }

    public void addQues(String content, String sheetName) {
        Queue<String> contents = addObj.computeIfAbsent(sheetName, v -> new LinkedList<>());
        contents.add(content);
    }

    public boolean importQues(String sheetName) {
        Queue<String> contents = addObj.get(sheetName);  // 获取队列，并从map中删除
        List<String> contentList = new ArrayList<>();
        while (CollUtil.isNotEmpty(contents)) {
            contentList.add(contents.poll());
        }
        questionnaireMapper.addQues(sheetName, contentList);
        addObj.remove(sheetName);  // 获取队列，并从map中删除
        return true;
    }

    @SneakyThrows
    public void exportQues(String sheetName, HttpServletResponse response) {
        // 从数据库中查询
        List<Questionnaire> list = questionnaireMapper.queryQues(sheetName);
        // 创建工作簿
        try (HSSFWorkbook workbook = new HSSFWorkbook()) {
            workbook.createFont();
            HSSFSheet sheet = workbook.createSheet("Sheet1");
            HSSFRow row = sheet.createRow(0);
            row.createCell(0).setCellValue("id");
            row.createCell(1).setCellValue("content");
            for (int i = 0; i < list.size(); i++) {
                row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(list.get(i).getId());
                row.createCell(1).setCellValue(list.get(i).getContent());
            }
            // 创建文件
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename=" + sheetName + ".xlsx");
            workbook.write(response.getOutputStream());
        }
    }

    public boolean updateQues(String json) {
        JSONObject jsonObject = JSONUtil.parseObj(json);
        HashMap<String, Object> map = new HashMap<>();
        map.put("ques", jsonObject.get("ques", Questionnaire.class));
        map.put("sheetName", jsonObject.getStr("sheetName"));
        return questionnaireMapper.updateQues(map) == 1;
    }

    public boolean deleteQues(int id, String sheetName) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("sheetName", sheetName);
        map.put("id", id);
        return questionnaireMapper.deleteQues(map) == 1;
    }
}
